Skip to content

Conversation

@frank-king
Copy link
Contributor

@frank-king frank-king commented Jul 27, 2025

This PR is part of the pin_ergonomics experiment (the tracking issue is #130494). It allows implementing Drop with a pinned self receiver, which is required for safe pin-projection.

Implementations:

  • At least and at most one of drop and pin_drop should be implemented.
  • No direct call of drop or pin_drop. They should only be called by the drop glue.
  • pin_drop must and must only be used with types that support pin-projection (i.e. types with #[pin_v2]).
  • Allows writing fn drop(&pin mut self) and desugars to fn pin_drop(&pin mut self). (Will be in the next PRs)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 27, 2025
@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@frank-king frank-king changed the title Implement Drop::pin_drop for !Unpin types Add Drop::pin_drop for pinned drops Sep 20, 2025
@rust-log-analyzer

This comment has been minimized.

@frank-king frank-king force-pushed the feature/pin-drop branch 2 times, most recently from d6ddfcf to 7b4bb5c Compare September 20, 2025 14:42
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@frank-king
Copy link
Contributor Author

The CI failed because Drop::drop becomes a provided method. I'm afraid it might not be a good way to hack into librustdoc. Is there any other good way to fix it?

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@Dylan-DPC Dylan-DPC added S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 7, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@frank-king frank-king force-pushed the feature/pin-drop branch 2 times, most recently from 6303109 to 9618d10 Compare November 7, 2025 12:42
@frank-king frank-king marked this pull request as ready for review November 7, 2025 14:18
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2025

⚠️ Warning ⚠️

@bors
Copy link
Collaborator

bors commented Nov 10, 2025

☔ The latest upstream changes (presumably #148397) made this pull request unmergeable. Please resolve the merge conflicts.

hir_analysis_coercion_between_struct_single_note = expected a single field to be coerced, none found
hir_analysis_conflict_impl_drop_and_pin_drop = conflict implementation of `Drop::drop` and `Drop::pin_drop`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hir_analysis_conflict_impl_drop_and_pin_drop = conflict implementation of `Drop::drop` and `Drop::pin_drop`
hir_analysis_conflict_impl_drop_and_pin_drop = conflicting implementations of `Drop::drop` and `Drop::pin_drop`

if tcx.is_lang_item(trait_id, LangItem::Drop)
// Allow calling `Drop::pin_drop` in `Drop::drop`
&& let Some(parent) = tcx.opt_parent(body_id)
&& !tcx.is_lang_item(parent, LangItem::Drop)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&& !tcx.is_lang_item(parent, LangItem::Drop)
&& !tcx.is_lang_item(tcx.parent(body_id), LangItem::Drop)

Body id should have a parent, but if it somehow doesn't, we certainly shouldn't skip the code below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, calling the destructor explicitly should probably be unsafe?

let mut pin_drop_span = None;
for &impl_item_id in item_impl.items {
let impl_item = tcx.hir_impl_item(impl_item_id);
if let hir::ImplItemKind::Fn(fn_sig, _) = impl_item.kind {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some HIR is accessed on a good path here, ideally queries should be used and HIR accesses should be avoided for better incremental compilation (or only performed when errors are reported).

Copy link
Contributor Author

@frank-king frank-king Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean using queries like asspciated_items or I should add a new query like this?

rustc_queries! {
    query drop_impl_fn_items(impl_id: DefId) -> DropImplFnItems { ... }
}
pub struct DropImplFnItems {
    pub drop: Option<DefId>,
    pub pin_drop: Option<DefId>,
}

Or let the query return spans of drop and pin_drop directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using existing queries like associated_items should be enough.

--> $DIR/missing-drop-method.rs:2:1
|
LL | impl Drop for DropNoMethod {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `drop` in implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can report the old error if tcx.features().pin_ergonomics() is not enabled?

@petrochenkov
Copy link
Contributor

The src/doc/book submodule update should be removed.

@petrochenkov
Copy link
Contributor

Let's run benchmarks first.
I'm mostly concerned about a new method being added to Drop, which increases the amount of codegen and sizes of vtables.

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rust-bors
Copy link

rust-bors bot commented Nov 12, 2025

🔒 Merge conflict

This pull request and the base branch diverged in a way that cannot
be automatically merged. Please rebase on top of the latest base
branch, and let the reviewer approve again.

How do I rebase?

Assuming self is your fork and upstream is this repository,
you can resolve the conflict following these steps:

  1. git checkout feature/pin-drop (switch to your branch)
  2. git fetch upstream HEAD (retrieve the latest base branch)
  3. git rebase upstream/HEAD -p (rebase on top of it)
  4. Follow the on-screen instruction to resolve conflicts (check git status if you got lost).
  5. git push self feature/pin-drop --force-with-lease (update this PR)

You may also read
Git Rebasing to Resolve Conflicts by Drew Blessing
for a short tutorial.

Please avoid the "Resolve conflicts" button on GitHub.
It uses git merge instead of git rebase which makes the PR commit history more difficult to read.

Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Cargo.lock conflict is
handled during merge and rebase. This is normal, and you should still perform step 5 to update this PR.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 12, 2025
@petrochenkov
Copy link
Contributor

Needs a rebase for perf run.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 12, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@petrochenkov petrochenkov removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 12, 2025
@frank-king
Copy link
Contributor Author

The src/doc/book submodule update should be removed.

One test would fail without that change, see
#144537 (comment).

@petrochenkov
Copy link
Contributor

The src/doc/book submodule update should be removed.

One test would fail without that change, see #144537 (comment).

Then the change needs to be merged into the book's master first (right now I'm not sure where the new submodule is pointing to).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants